layui.use(['table', 'form', 'jquery','layer'], function() {
    let  table = layui.table;
    let  form = layui.form;
    let  $ = layui.jquery;
    let  layer = layui.layer;

    let  MODULE_PATH = "/admin.php/";

    let  cols = [
        [{
            type: 'checkbox'
        },
            {
                title: '编号',
                field: 'id',
                width: '10%',
                align: 'center'
            },
            {
                title: '媒体名称',
                field: 'name',
                width: '10%',
                align: 'center'
            },
            {
                title: '说明',
                field: 'explain',
                align: 'center'
            },
            {
                title: '状态',
                field: 'status',
                align:'center',
                width: '10%',
                templet:'#role-enable'
            },
            {
                title: '操作',
                toolbar: '#organization-bar',
                width: '15%',
                align: 'center',
            }
        ]
    ]

    //数据对象
    let  tableData;

    //数据加载
    table.render({
        id:"source_table",
        elem: '#source-table',
        url: MODULE_PATH + 'service.source/query',
        page: true,
        cols: cols,
        skin: 'line',
        toolbar: '#source-toolbar',
        loading: true,
        text: {
            none : '空空如也'
        },
        response: {
            statusCode: 200,
        },
        done: function(res,curr,count) {
            tableData = table.cache.source_table;
            //定义验证
            let  validate = ['',null,0,undefined,'NAN',[]];
            var that = this.elem.next();
            var data = res.data;
            if (data) {
                data.forEach((item,index) => {
                    var colorId = localStorage.getItem("theme-color")
                    if (index % 2 == 0) {
                        tr = that.find('.layui-table-box tbody tr[data-index='+ index +']').css('background-color',colourDeal(colorId));
                    } else {
                        tr = that.find('.layui-table-box tbody tr[data-index='+ index +']').css('background-color');
                    }
                });
            }
            //判断表格中是否存在数据
            if ( (validate.indexOf(data) >= 0) && curr != 1 ) {
                var cr = curr > 1 ? ((curr * 1) - 1) : 1;
                //重载
                table.reload('source_table',{
                    page : {
                        curr : cr
                    }
                });
            }
        },
        defaultToolbar: [{
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter']
    });

    //tool中的按钮
    table.on('tool(source-table)', function(obj) {
        if (obj.event === 'remove') {
            window.remove(obj);
        } else if (obj.event === 'edit') {
            window.edit(obj);
        }
    });

    //在toolbar中的按钮
    table.on('toolbar(source-table)', function(obj) {
        if (obj.event === 'add') {
            window.add();
        } else if (obj.event === 'refresh') {
            window.refresh();
        } else if (obj.event === 'batchRemove') {
            window.batchRemove(obj);
        }
    });

    //增加
    window.add = () => {
        layer.open({
            type: 2,
            area: ['500px', '450px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: '媒体增加',
            btnAlign: 'c',
            content: MODULE_PATH + 'service.source/add',
            success: (layero, index) => {
                var iframe = window['layui-layer-iframe' + index];
                iframe.add();
            }
        });
    };

    //修改
    window.edit = (obj) => {
        layer.open({
            type: 2,
            area: ['500px', '450px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: '媒体修改',
            btnAlign: 'c',
            content: MODULE_PATH + 'service.source/edit?id=' + obj.data.id,
            success: (layero, index) => {
                var iframe = window['layui-layer-iframe' + index];
                iframe.edit(obj.data.id);
            }
        });
    }

    //批量删除
    window.batchRemove = (obj) => {
        let  data = table.checkStatus(obj.config.id).data;
        if (data.length === 0) {
            layer.msg("未选中数据", {
                icon: 3,
                shade: 0.1,
                time: 1000
            });
            return false;
        }
        let  ids = [];
        for (let  i = 0; i < data.length; i++) {
            ids.push(data[i].id);
        }
        layer.confirm('确定要删除这些用户', {
            icon: 3,
            shade: 0.1,
            title: '提示'
        }, function(index) {
            layer.close(index);
            let  loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "service.source/del",
                dataType: 'json',
                type: 'POST',
                data: {
                    'id' : ids
                },
                success: function(res) {
                    layer.close(loading);
                    //返回结果处理
                    window.result(res.code,res.msg);
                }
            })
        });
    }

    //删除
    window.remove = (obj) => {
        layer.confirm('确定要删除该用户', {
            icon: 3,
            title: '提示'
        }, (index) => {
            layer.close(index);
            let  loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "service.source/del",
                dataType: 'json',
                type: 'POST',
                data: {
                    id : obj.data['id']
                },
                success: function(res) {
                    layer.close(loading);
                    //返回结果处理
                    window.result(res.code,res.msg);
                }
            })
        });
    }

    //操作返回结果处理函数
    window.result = (code,msg) => {
        if (code == 200) {
            //数据刷新
            table.reload('source_table');
            layer.msg(msg, { icon: 1, offset: "auto", time:2000 });
        } else {
            layer.msg(msg, { icon: 2, offset: "auto", time:2000 });
        }
    }

    //重置
    window.refresh = () => {
        //数据刷新
        table.reload('source_table');
    }

    //状态修改
    form.on('switch(source-enable)', function(obj) {
        //根据业务判断是开启还是关闭
        var state = obj.elem.checked == true ? 1 : 0;
        //获取索引
        var index  = obj.othis.parents('tr').attr("data-index");
        //获取id
        var id = tableData[index].id;
        //数据获取
        var data = {
            'id' : id,
            'status' : state
        };
        //发送ajax
        $.ajax({
            url : MODULE_PATH + 'service.source/edit',
            type : 'POST',
            dataType : 'json',
            data : data,
            success : function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1, offset: "auto", time:2000 });
                } else {
                    layer.msg(res.msg, { icon: 2, offset: "auto", time:2000 });
                }
            }
        });
    });
});